package com.demo.common.excel.convert;

import cn.hutool.core.util.ObjUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.demo.common.utils.ApplicationContextHolder;
import com.demo.entity.DicValue;
import com.demo.service.DicValueService;
import org.springframework.util.ObjectUtils;

public class AppellationConverter implements Converter<Integer> {
    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData,
                                     ExcelContentProperty contentProperty,
                                     GlobalConfiguration globalConfiguration) throws Exception {
        // 获取单元格内容
        String cellDataValue = cellData.getStringValue();
        // 获取spring容器中指定的bean对象
        DicValueService dicValueService = ApplicationContextHolder.getBean(DicValueService.class);
        // 根据数据字典值名称获取数据字典 id
        DicValue dicValue = dicValueService.getOne(new LambdaQueryWrapper<DicValue>()
                .eq(DicValue::getTypeValue, cellDataValue));
        if (ObjUtil.isNotEmpty(dicValue)){
            return dicValue.getId();
        }
        // 没有返回 -1
        return -1;
    }
}
